markdown-text-editor 2.0.0-beta.2 → 2.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2397,6 +2397,14 @@ var jn = An(), X = class {
2397
2397
  constructor(e, t) {
2398
2398
  this.editor = e, this.defaultText = `${t} text`, this.button = this.createButton(), this.title = t;
2399
2399
  }
2400
+ trimSelection() {
2401
+ let e = this.editor.usertextarea, { selectionStart: t, selectionEnd: n, value: r } = e;
2402
+ if (t === n) return;
2403
+ let i = t, a = n;
2404
+ for (; i < a && /\s/.test(r[i]);) i++;
2405
+ for (; a > i && /\s/.test(r[a - 1]);) a--;
2406
+ i !== a && (i !== t || a !== n) && e.setSelectionRange(i, a);
2407
+ }
2400
2408
  createButton(e) {
2401
2409
  let t = document.createElement("button"), n = this.title ? `${this.title.replace(/ /g, "-")}-btn`.toLowerCase() : "";
2402
2410
  t.innerHTML = e;
@@ -2412,6 +2420,7 @@ var jn = An(), X = class {
2412
2420
  super(e, "Bold"), this.button = this.createButton("\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path stroke=\"none\" d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M7 5h6a3.5 3.5 0 0 1 0 7h-6l0 -7\" /><path d=\"M13 12h1a3.5 3.5 0 0 1 0 7h-7v-7\" /></svg>\n ");
2413
2421
  }
2414
2422
  applySyntax() {
2423
+ this.trimSelection();
2415
2424
  let e = this.editor.usertextarea, { selectionStart: t, selectionEnd: n } = e, r = e.value, i = r.substring(t, n), a = "", o = 0;
2416
2425
  if (i.startsWith("***") && i.endsWith("***")) a = "*" + i.slice(3, -3) + "*";
2417
2426
  else if (i.startsWith("**") && i.endsWith("**")) a = i.slice(2, -2);
@@ -2429,6 +2438,7 @@ var jn = An(), X = class {
2429
2438
  super(e, "Italic"), this.button = this.createButton("\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path stroke=\"none\" d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M11 5l6 0\" /><path d=\"M7 19l6 0\" /><path d=\"M14 5l-4 14\" /></svg>\n ");
2430
2439
  }
2431
2440
  applySyntax() {
2441
+ this.trimSelection();
2432
2442
  let e = this.editor.usertextarea, { selectionStart: t, selectionEnd: n } = e, r = e.value, i = r.substring(t, n), a = "", o = 0;
2433
2443
  if (i.startsWith("***") && i.endsWith("***")) a = "**" + i.slice(3, -3) + "**";
2434
2444
  else if (i.startsWith("*") && i.endsWith("*") && !i.startsWith("**")) a = i.slice(1, -1);
@@ -2446,6 +2456,7 @@ var jn = An(), X = class {
2446
2456
  super(e, "Strikethrough"), this.button = this.createButton("\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path stroke=\"none\" d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M5 12l14 0\" /><path d=\"M16 6.5a4 2 0 0 0 -4 -1.5h-1a3.5 3.5 0 0 0 0 7h2a3.5 3.5 0 0 1 0 7h-1.5a4 2 0 0 1 -4 -1.5\" /></svg>\n ");
2447
2457
  }
2448
2458
  applySyntax() {
2459
+ this.trimSelection();
2449
2460
  let e = this.editor.usertextarea, { selectionStart: t, selectionEnd: n } = e, r = e.value, i = r.substring(t, n), a = "", o = 0;
2450
2461
  if (i.startsWith("~~") && i.endsWith("~~")) a = i.slice(2, -2);
2451
2462
  else if (r.substring(t - 2, t) === "~~" && r.substring(n, n + 2) === "~~") {
@@ -2563,6 +2574,7 @@ var zn = class extends X {
2563
2574
  super(e, "Link"), this.button = this.createButton("\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path stroke=\"none\" d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M9 15l6 -6\" /><path d=\"M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464\" /><path d=\"M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463\" /></svg>\n ");
2564
2575
  }
2565
2576
  applySyntax(e) {
2577
+ this.trimSelection();
2566
2578
  let t = this.editor, n = t.usertextarea, { selectionStart: r, selectionEnd: i } = n, a = n.value, o = a.substring(0, r), s = a.substring(i);
2567
2579
  if (o.endsWith("[") && /^\]\([^)]+\)/.test(s)) {
2568
2580
  let e = s.match(/^\]\(([^)]+)\)/);
@@ -2764,6 +2776,7 @@ var Un = class extends X {
2764
2776
  super(e, "Code"), this.button = this.createButton("\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path stroke=\"none\" d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M7 8l-4 4l4 4\" /><path d=\"M17 8l4 4l-4 4\" /><path d=\"M14 4l-4 16\" /></svg>\n ");
2765
2777
  }
2766
2778
  applySyntax() {
2779
+ this.trimSelection();
2767
2780
  let e = this.editor.usertextarea, { selectionStart: t, selectionEnd: n } = e, r = e.value, i = r.substring(t, n);
2768
2781
  if (i.startsWith("`") && i.endsWith("`")) {
2769
2782
  this.editor.insertText(i.slice(1, -1), 0, 0);
@@ -3607,7 +3620,7 @@ var Mr = class {
3607
3620
  }
3608
3621
  }, Nr = class {
3609
3622
  constructor(e) {
3610
- this.editor = e, this._matches = [], this._currentIndex = -1, this._caseSensitive = !1, this._panel = null, this._findInput = null, this._replaceInput = null, this._countEl = null, this._replaceRow = null, this._caseSensitiveBtn = null, this._highlightEl = null, this._scrollHandler = null, this._taHandler = this._onTextareaKeyDown.bind(this), e.usertextarea.addEventListener("keydown", this._taHandler);
3623
+ this.editor = e, this._matches = [], this._currentIndex = -1, this._caseSensitive = !1, this._panel = null, this._findInput = null, this._replaceInput = null, this._countEl = null, this._replaceRow = null, this._caseSensitiveBtn = null, this._highlightEl = null, this._matchTop = null, this._matchHeight = 0, this._scrollHandler = null, this._taHandler = this._onTextareaKeyDown.bind(this), e.usertextarea.addEventListener("keydown", this._taHandler);
3611
3624
  }
3612
3625
  _onTextareaKeyDown(e) {
3613
3626
  let t = e.ctrlKey || e.metaKey;
@@ -3686,16 +3699,13 @@ var Mr = class {
3686
3699
  this._matches.length !== 0 && (this._currentIndex = (this._currentIndex - 1 + this._matches.length) % this._matches.length, this._selectCurrent(), this._updateCount());
3687
3700
  }
3688
3701
  _selectCurrent() {
3689
- if (this._currentIndex === -1 || this._matches.length === 0) {
3690
- this._clearMatchHighlight();
3691
- return;
3692
- }
3693
- let { start: e } = this._matches[this._currentIndex];
3694
- this._scrollToMatch(this.editor.usertextarea, e), this._showMatchHighlight();
3702
+ this._currentIndex === -1 || this._matches.length === 0 ? this._clearMatchHighlight() : (this._showMatchHighlight(), this._scrollMatchIntoView());
3695
3703
  }
3696
- _scrollToMatch(e, t) {
3697
- let n = parseInt(window.getComputedStyle(e).lineHeight) || 20, r = e.value.substring(0, t).split("\n").length - 1, i = Math.floor(e.clientHeight / n);
3698
- e.scrollTop = Math.max(0, (r - Math.floor(i / 2)) * n);
3704
+ _scrollMatchIntoView() {
3705
+ let e = this.editor.usertextarea;
3706
+ if (this._matchTop === null || this._matchHeight <= 0) return;
3707
+ let t = e.clientHeight, n = e.scrollTop, r = this._matchHeight, i = n;
3708
+ this._matchTop - r < n ? i = this._matchTop - r : this._matchTop + this._matchHeight + r > n + t && (i = this._matchTop + this._matchHeight + r - t), i !== n && (e.scrollTop = Math.max(0, i), this._highlightEl && (this._highlightEl.style.top = this._matchTop - e.scrollTop + "px"));
3699
3709
  }
3700
3710
  _showMatchHighlight() {
3701
3711
  if (this._clearMatchHighlight(), this._currentIndex === -1 || !this._matches.length) return;
@@ -3706,7 +3716,7 @@ var Mr = class {
3706
3716
  pointerEvents: "none",
3707
3717
  top: "0",
3708
3718
  left: "0",
3709
- right: "0",
3719
+ width: n.clientWidth + "px",
3710
3720
  whiteSpace: "pre-wrap",
3711
3721
  wordBreak: "break-word",
3712
3722
  overflowWrap: "break-word",
@@ -3721,7 +3731,9 @@ var Mr = class {
3721
3731
  let o = document.createElement("span");
3722
3732
  o.textContent = n.value.substring(e, t) || "​", a.appendChild(document.createTextNode(n.value.substring(0, e))), a.appendChild(o), a.appendChild(document.createTextNode(n.value.substring(t))), i.appendChild(a);
3723
3733
  try {
3724
- let e = i.getBoundingClientRect(), t = o.getBoundingClientRect(), r = document.createElement("div");
3734
+ let e = i.getBoundingClientRect(), t = o.getBoundingClientRect();
3735
+ this._matchTop = t.top - e.top, this._matchHeight = t.height;
3736
+ let r = document.createElement("div");
3725
3737
  Object.assign(r.style, {
3726
3738
  position: "absolute",
3727
3739
  top: t.top - e.top - n.scrollTop + "px",
@@ -3738,7 +3750,7 @@ var Mr = class {
3738
3750
  }
3739
3751
  }
3740
3752
  _clearMatchHighlight() {
3741
- this._highlightEl?.remove(), this._highlightEl = null;
3753
+ this._highlightEl?.remove(), this._highlightEl = null, this._matchTop = null;
3742
3754
  }
3743
3755
  _updateCount() {
3744
3756
  this._countEl && (this._matches.length === 0 ? this._countEl.textContent = this._findInput?.value ? "No results" : "" : this._countEl.textContent = `${this._currentIndex + 1} of ${this._matches.length}`);
@@ -3876,20 +3888,23 @@ var Mr = class {
3876
3888
  this._autoGrow(), this.renderHybrid(), this.debouncedPreview(), this.notifyChange();
3877
3889
  }, this._scrollHandler = () => {
3878
3890
  this._scrollRaf ||= requestAnimationFrame(() => {
3879
- if (this._scrollRaf = null, this.mode === "hybrid" && (this.displayLayer.scrollTop = this.usertextarea.scrollTop, this.displayLayer.scrollLeft = this.usertextarea.scrollLeft), this.preview && this.previewContent) {
3880
- let e = this.usertextarea, t = e.scrollTop / (e.scrollHeight - e.clientHeight);
3881
- this.previewContent.scrollTop = t * (this.previewContent.scrollHeight - this.previewContent.clientHeight);
3882
- }
3891
+ this._scrollRaf = null, this.mode === "hybrid" && (this.displayLayer.scrollTop = this.usertextarea.scrollTop, this.displayLayer.scrollLeft = this.usertextarea.scrollLeft), this.preview && this.previewContent && this._syncPreviewScroll();
3883
3892
  });
3884
3893
  }, this.usertextarea.addEventListener("input", this._inputHandler), this.usertextarea.addEventListener("scroll", this._scrollHandler);
3885
3894
  }
3886
3895
  debouncedPreview() {
3887
3896
  this.preview && (clearTimeout(this.previewTimer), this.previewTimer = setTimeout(() => {
3888
- this.previewContent.innerHTML = this._renderMarkdown(this.usertextarea.value), this.wirePreviewCheckboxes();
3889
- let e = this.usertextarea, t = e.scrollTop / (e.scrollHeight - e.clientHeight);
3890
- this.previewContent.scrollTop = t * (this.previewContent.scrollHeight - this.previewContent.clientHeight);
3897
+ let e = this.previewContent.scrollTop;
3898
+ this.previewContent.innerHTML = this._renderMarkdown(this.usertextarea.value), this.wirePreviewCheckboxes(), this._syncPreviewScroll() || (this.previewContent.scrollTop = e);
3891
3899
  }, 150));
3892
3900
  }
3901
+ _syncPreviewScroll() {
3902
+ if (!this.preview || !this.previewContent) return !1;
3903
+ let e = this.usertextarea, t = e.scrollHeight - e.clientHeight;
3904
+ if (t <= 0) return !1;
3905
+ let n = e.scrollTop / t, r = this.previewContent.scrollHeight - this.previewContent.clientHeight;
3906
+ return this.previewContent.scrollTop = n * r, !0;
3907
+ }
3893
3908
  renderMarkdown(e) {
3894
3909
  return this._renderMarkdown(e);
3895
3910
  }